# -*- coding:utf-8 -*-
'''
Created on 2013-10-20

@author: Administrator
'''
import MySQLdb
from MySQLdb.constants.FIELD_TYPE import *
from StringIO import StringIO

class UiObject(object):
    
    def __init__(self):
        pass
    
    def __str__(self):
        return  str(self.__dict__)
    
    def __repr__(self, *args, **kwargs):
        builder = StringIO()
        builder.write("{")
        i = 0
        l = len(self.__dict__)
        for key in self.__dict__:
            builder.write("'%s': '%s'" % (key, str(self.__dict__[key])))
            if i != l-1:
                builder.write(',')
            i += 1
        builder.write('}')
        return builder.getvalue()

class DBManager(object):
    '''
    classdocs
    '''


    def __init__(self, host, port, user, passwd, db):
        '''
        Constructor
        '''
        self.db = MySQLdb.connect(host=host, port=port, user=user, passwd=passwd, db=db)
        self.cursor = self.db.cursor()
        self.cursor.execute('set names utf8')

if __name__ == '__main__':
    db_manager = DBManager('127.0.0.1', 3306, 'root', '860728', "ds")
    db_manager.cursor.execute("select * from f_word limit 50")
    for description in db_manager.cursor.description:
        print description
    
    datas = []
    description = db_manager.cursor.description
    for row in db_manager.cursor.fetchall():
        uiObject = UiObject()
        for i in range(len(description)):
            setattr(uiObject, description[i][0], row[i])
        datas.append(uiObject)
    
    la = [u'我是中国人']
    
    print datas
        
